package com.quasar.hpatient.view.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.chat.EMClient;
import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseFragment;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.event.UpdateEvent;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.medicalmodel.DoctorListBean;
import com.quasar.hpatient.onInterface.OnDoctorListener;
import com.quasar.hpatient.presenter.DoctorFindPresenter;
import com.quasar.hpatient.view.LoginActivity_;
import com.quasar.hpatient.view.adapter.DoctorAdapter;
import com.quasar.hpatient.view.doctor.DoctorDetailsActivity_;
import com.quasar.hpatient.view.doctor.DoctorFindActivity_;
import com.quasar.hpatient.widget.SideBarView;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Collections;
import java.util.Comparator;

import chatui.ui.ChatActivity;

/**
 * author : 许建宏
 * Date: 2017/9/29 0029
 * View:我的医生
 */
@EFragment(R.layout.fragment_doctor)
public class DoctorFragment extends BaseFragment implements OnDoctorListener {

    public DoctorAdapter doctorAdapter;
    //设置分页
    private int page = 1;
    @ViewById(R.id.side_bar)
    SideBarView side_bar;

    @ViewById(R.id.rv_list)
    RecyclerView rv_list;
    private DoctorFindPresenter doctorFindPresenter;
    @ViewById(R.id.ll_left_text)
    TextView ll_left_text;
    @ViewById(R.id.iv_left)
    ImageView iv_left;
    @ViewById(R.id.iv_right)
    TextView iv_right;

    @AfterViews
    void init() {
        initView();
        initData();
    }

    //初始化adapter布局
    public void initAdapter() {
        iv_left.setVisibility(View.GONE);
        ll_left_text.setVisibility(View.VISIBLE);
        ll_left_text.setText("我的医生");
        iv_right.setText("添加医生");
        doctorAdapter = new DoctorAdapter();
        rv_list.setAdapter(doctorAdapter);
        doctorFindPresenter.MyDoctorxData(page + "");
        //item点击
        doctorAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                switch (view.getId()) {
                    case R.id.tv_communication:
                        //环信发送好友界面
                        getContext().startActivity(new Intent(getContext(), ChatActivity.class).putExtra("userId", doctorAdapter.getItem(position).getBmobUserName()));
                        break;

                    case R.id.headimg:
                        DoctorDetailsActivity_.intent(getActivity()).doctor(doctorAdapter.getItem(position)).start();
                        break;
                }
            }
        });
    }

    @Override
    public void initView() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        rv_list.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    //添加医生
    @Click(R.id.ll_right)
    public void ll_right() {
        DoctorFindActivity_.intent(getActivity()).start();
    }

    @Override
    public void initData() {
        doctorFindPresenter = new DoctorFindPresenter(this);
        initAdapter();
    }

    @Override
    public void onMyDoctorSuccess(final AnotherResult<DoctorListBean> data) {
        //按照字母排序
        Collections.sort(data.getList(), new Comparator<DoctorListBean>() {
            @Override
            public int compare(DoctorListBean o1, DoctorListBean o2) {
                if (o2.getIndex().equals("#") ||o1.getIndex().equals("@")) {
                    return -1;
                } else if (o1.getIndex().equals("#") || o2.equals("@")) {
                    return 1;
                } else {
                    return o1.getIndex().compareTo(o2.getIndex());
                }
            }
        });
        doctorAdapter.setNewData(data.getList());

        side_bar.setOnSelectIndexItemListener(new SideBarView.OnSelectIndexItemListener() {
            @Override
            public void onSelectIndexItem(String index) {
                for (int i = 0; i < data.getList().size(); i++) {
                    if (data.getList().get(i).getIndex().equals(index)) {
                        ((LinearLayoutManager) rv_list.getLayoutManager()).scrollToPositionWithOffset(i, 0);
                        return;
                    }
                }
            }
        });

    }

    @Override
    public void onDoctorListSuccess(AnotherResult<DoctorListBean> data) {

    }

    @Override
    public void onError(String msg) {
        if (msg != null) {
            msg(msg);
            if (msg.equals("0x00000031")) {
                EMClient.getInstance().logout(true); //登陆前先退出，保证登陆成功！
                LoginActivity_.intent(getActivity()).start();
            }
        }

    }

    @Override
    public void onRefreshUI() {
        doctorAdapter.notifyDataSetChanged();
    }

    @Override
    public void onSubmitSuccess(AnotherResult<DoctorListBean> dataStateResult) {

    }

    //刷新我的医生列表
    //刷新界面
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UpdateEvent updateEvent) {
        page = 1;
        if (updateEvent.type.equals(ConstantManger.MYDOCTORX)) {
            doctorFindPresenter.MyDoctorxData(page + "");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDeleteDoctorx(DataStateResult dataStateResult) {

    }

    @Override
    public void onApplyDoctorx(DataStateResult dataStateResult) {

    }
}
